home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / hpcopy.com / HPCOPY.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1989-04-24  |  9.9 KB  |  257 lines

  1. Unit HPCopy;
  2.  
  3. { This unit  is designed  to dump  graphics  images produced }
  4. { by  Turbo  Pascal  5.0's  Graph  Unit to a Hewlett-Packard }
  5. { LaserJet printer (on LPT1 by default; change in Unit       }
  6. { initialization sequence at bottom of unit)                 }
  7. {                                                            }
  8. { CAUTION: you cannot link the TP5 Prn unit in with this     }
  9. { unit (or any program linked with this unit) since this     }
  10. { program redefines Lst as its own. No problem, though just  }
  11. { don't use the TP Prn unit...                               }
  12. {                                                            }
  13. { You can set the  Aspect Ratio to whatever you like before  }
  14. { drawing things on the screen. For a VGA and a LaserJet II  }
  15. { the default aspect ratio for 100 dpi is almost perfect.    }
  16. {                                                            }
  17. { For other graphics adapters, you may need to set the       }
  18. { ratio to something to give a proper looking hard copy. If  }
  19. { the Aspect Ratio is NOT set, the image produced by this    }
  20. { routine *may* appear Ellipsoidal.                          }
  21. {                                                            }
  22. { The basic recommended sequence is something like:          }
  23. {     GetAspectRatio(old_xasp,old_yasp);                     }
  24. {       this gets old aspect ratio for your video adapter    }
  25. {     SetAspectRatio(new_xasp,new_yasp);                     }
  26. {       this sets a new (empirically determined) aspect      }
  27. {       ratio to get hardcopy to look right                  }
  28. {     (do what ever graphics to the screen you wish)         }
  29. {     HPHardCopy;                                            }
  30. {       make the hard copy with the adjusted ratio           }
  31. {     SetAspectRatio(old_xasp,old_yasp);                     }
  32. {       reset back to the "correct" aspect ratio             }
  33. {                                                            }
  34. {  READ COMMENTS BELOW BEFORE USING!!!!!!!!!                 }
  35. {                                                            }
  36.  
  37.  
  38.  
  39. Interface
  40.  
  41. Uses Crt, Dos, Graph;
  42.  
  43. Var
  44.    LST : Text;     { MUST Redefine because Turbo's Printer }
  45.                     { Unit does not open LST with the File }
  46.                     { Mode as BINARY.                       }
  47.  
  48. Procedure HPHardCopy(Rotate90 : Boolean);
  49. { Procedure to be  called when  the desired image is on the }
  50. { screen.                                                   }
  51.  
  52. Implementation
  53.  
  54. Var
  55.    Width, Height : Word; { Variables used to store settings }
  56.    Vport : ViewPortType; { Used in the call GetViewSettings }
  57.  
  58. {$F+}
  59. Function LSTNoFunction ( Var F : TextRec ) : Integer;
  60. { This  function performs a NUL  operation  for a  Reset or }
  61. { Rewrite on LST.                                          }
  62.  
  63. Begin
  64.    LSTNoFunction := 0;
  65. End;
  66.  
  67. Function LSTOutPutToPrinter( Var F : TextRec ) : Integer;
  68. { LSTOutPutToPrinter sends the output to the Printer port }
  69. { number stored in the first byte or the  UserData area of }
  70. { the Text Record.                                         }
  71.  
  72. Var
  73.    Regs : Registers;
  74.    P : Word;
  75.  
  76. Begin
  77.    With F Do
  78.    Begin
  79.       P := 0;
  80.       Regs.AH := 16;
  81.       While( P < BufPos ) and ( ( Regs.AH And 16 ) = 16 ) Do
  82.       Begin
  83.          Regs.AL := Ord( BufPtr^[P] );
  84.          Regs.AH := 0;
  85.          Regs.DX := UserData[1];
  86.          Intr( $17, Regs );
  87.          Inc( P );
  88.       End;
  89.       BufPos := 0;
  90.    End;
  91.    If( ( Regs.AH And 16 ) = 16 ) Then
  92.       LSTOutPutToPrinter := 0         { No Error           }
  93.    Else
  94.       If( ( Regs.AH And 32 ) = 32 ) Then
  95.          LSTOutPutToPrinter := 159    { Out of Paper       }
  96.       Else
  97.          LSTOutPutToPrinter := 160;   { Device Write Fault }
  98. End;
  99. {$F-}
  100.  
  101. Procedure AssignLST( Port : Byte );
  102. { AssignLST both sets up the LST text file record as would }
  103. { ASSIGN, and initializes it as would a RESET.             }
  104. {                                                          }
  105. { The parameter  passed to this  procedure  corresponds to }
  106. { DOS's LPT  number.  It is set to 1 by  default, but  can }
  107. { easily  be  changed to any  LPT number  by  changing the }
  108. { parameter  passed  to  this  procedure  in  this  unit's }
  109. { initialization code.                                     }
  110.  
  111. Begin
  112.    With TextRec( LST ) Do
  113.    Begin
  114.       Handle := $FFF0;
  115.       Mode := fmOutput;
  116.       BufSize := SizeOf( Buffer );
  117.       BufPtr := @Buffer;
  118.       BufPos := 0;
  119.       OpenFunc := @LSTNoFunction;
  120.       InOutFunc := @LSTOutPutToPrinter;
  121.       FlushFunc := @LSTOutPutToPrinter;
  122.       CloseFunc := @LSTOutPutToPrinter;
  123.       UserData[1] := Port - 1;
  124.    End;
  125. End;
  126.  
  127. Procedure HPHardCopy;
  128. { Produces hard copy of a graph on Hewlett-Packard Laserjet }
  129. { printer By Joseph J. Hansen 9-15-87                       }
  130. { Modified Extensively for compatibility with Version 4.0's }
  131. { Graph Unit By Gary Stoker                                 }
  132. { Modified a little more for version 5 by David Holtkamp    }
  133. {                                                           }
  134. { Unlike Graphix Toolbox procedure HardCopy, this procedure }
  135. { has no parameters, though it could easily be rewritten to }
  136. { include  resolution in dots  per inch,  starting  column, }
  137. { inverse image, etc.                                       }
  138. {                                                           }
  139. { Modified just a bit by Kory Peterson                      }
  140. { 4/21/89                                                   }
  141. { This mod will print either Normal or LandScape on         }
  142. { the HP. Set Rotate90 to True for LandScape. Also with     }
  143. { the aspect ratio,  I found the setting of 75 dpi looked   }
  144. { better but play with it to find out your best one.        }
  145. { Circles where tested for both modes and they came out     }
  146. { looking just fine.                                        }
  147. {                      Kory                                 }
  148. {                                                           }
  149.  
  150. Const DotsPerInch  = '75';
  151.                     { 100 dots per inch  gives  full-screen }
  152.                     { width of 7.2 inches for Hercules card }
  153.                     { graphs, 6.4 inches for IBM color card }
  154.                     { and 6.4  inches  for EGA card.  Other }
  155.                     { allowable values are 75, 150, and 300.}
  156.                     { 75  dots  per  inch  will  produce  a }
  157.                     { larger full-screen graph which may be }
  158.                     { too  large to  fit  on an  8 1/2 inch }
  159.                     { page; 150 and 300  dots per inch will }
  160.                     { produce smaller graphs                }
  161.  
  162.       CursorPosition = '5';
  163.                     { Column position of left side of graph }
  164.       Esc            = #27;
  165.                     { Escape character                      }
  166.  
  167. Var LineHeader     : String[6];
  168.                     { Line  Header used for each  line sent }
  169.                     { to the LaserJet printer.              }
  170.     LineLength     : String[2];
  171.                     { Length  in  bytes of  the  line to be }
  172.                     { sent to the LaserJet.                 }
  173.     Y              : Integer;
  174.                     { Temporary loop Varible.               }
  175.  
  176. Procedure DrawLine ( Y : Integer );
  177. { Draws a single line of dots.  No of Bytes sent to printer }
  178. { is Width + 1.  Argument of the procedure is the row no, Y }
  179.  
  180. Var GraphStr       : String[128]; { String  used for OutPut }
  181.                                   { this is good up to 1024 }
  182.                                   { pixels wide in x        }
  183.     Base           : Word;        { Starting   position  of }
  184.                                   { output byte.            }
  185.     BitNo,                        { Bit Number worked on    }
  186.     ByteNo,                       { Byte number worked on   }
  187.     DataByte       : Byte;        { Data Byte being built   }
  188.  
  189. Begin
  190.   FillChar( GraphStr, SizeOf( GraphStr ), #0 );
  191.   GraphStr := LineHeader;
  192.   For ByteNo := 0 to Width  Do
  193.   Begin
  194.     DataByte := 0;
  195.     Base := 8 * ByteNo;
  196.     For BitNo := 0 to 7 Do
  197.     Begin
  198.       if NOT Rotate90 then begin
  199.          If GetPixel( BitNo+Base, Y ) > 0 Then begin
  200.                  DataByte := DataByte + 128 Shr BitNo;
  201.          end;
  202.       end
  203.       else   begin
  204.          If GetPixel( Y,BitNo+Base ) > 0 Then
  205.                  DataByte := DataByte + 128 Shr BitNo;
  206.       end;
  207.     End;
  208.     GraphStr := GraphStr + chr(DataByte)
  209.   End;
  210.   Write (Lst, GraphStr)
  211.  
  212. End; {Of Drawline}
  213.  
  214. Begin {Main procedure HPCopy}
  215.   FillChar( LineLength, SizeOf( LineLength ), #0 );
  216.   FillChar( LineHeader, SizeOf( LineHeader ), #0 );
  217.  
  218.   GetViewSettings( Vport );
  219.   IF NOT Rotate90 then begin
  220.      Width := ( Vport.X2 + 1 ) - Vport.X1;
  221.      Width := ( ( Width - 7 ) Div 8 );
  222.      Height := Vport.Y2 - Vport.Y1
  223.   end
  224.   else begin
  225.      Width := ( Vport.Y2 + 1 ) - Vport.Y1;
  226.      Width := ( ( Width - 7 ) Div 8 );
  227.      Height := Vport.X2 - Vport.X1
  228.   end;
  229.  
  230.   Write (LST, Esc + 'E');                 { Reset Printer   }
  231.   Write (LST, Esc+'*t'+DotsPerInch+'R');  { Set density in  }
  232.                                           { dots per inch   }
  233.   Write (LST, Esc+'&a'+CursorPosition+'C');{ Move cursor to }
  234.                                           { starting col    }
  235.   Write (LST, Esc + '*r1A');        { Begin raster graphics }
  236.  
  237.   Str (Width + 1, LineLength);
  238.   LineHeader := Esc + '*b' + LineLength + 'W';
  239.  
  240.  
  241.   if(Rotate90) then
  242.      For Y := Height + 1  downto 0 Do
  243.        DrawLine ( Y )
  244.   else
  245.      For Y := 0 to Height Do
  246.        DrawLine ( Y );
  247.  
  248.   Write (LST, Esc + '*rB');           { End Raster graphics }
  249.   Write (LST, Esc + 'E');             { Reset  printer  and }
  250.                                       { eject page          }
  251. End;
  252.  
  253. Begin                                 { Unit initialization }
  254.    AssignLST( 1 );
  255. (* Writeln( 'LST INITIALIZED!' ); *)  { diagnostic message  }
  256. End.
  257.